package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import qlsl.androiddesign.activity.commonactivity.VideoPlayActivity;
import qlsl.androiddesign.activity.subactivity.PostingCreateActivity;
import qlsl.androiddesign.activity.subactivity.PostingDetailActivity;
import qlsl.androiddesign.adapter.subadapter.PhotoGridAdapter;
import qlsl.androiddesign.adapter.subadapter.PostingCommentAdapter;
import qlsl.androiddesign.constant.BaseConstant;
import qlsl.androiddesign.entity.otherentity.FileType;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.http.service.commonservice.CommentService;
import qlsl.androiddesign.http.service.subservice.ForumService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshScrollView;
import qlsl.androiddesign.util.commonutil.ImageUtils;
import qlsl.androiddesign.util.commonutil.TimeFormatUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 帖子详情页<br/>
 * 需要传入的键：data<br/>
 * 传入的值类型： Posting<br/>
 * 传入的值含义：帖子详情<br/>
 * 是否必传 ：是
 */
public class PostingDetailView extends FunctionView<PostingDetailActivity> implements OnClickListener {

	private PullToRefreshScrollView refreshView;

	private ListView listView;

	private Posting item;

	public PostingDetailView(PostingDetailActivity activity) {
		super(activity);
		setContentView(R.layout.activity_posting_detail);
	}

	protected void initView(View view) {
		item = (Posting) activity.getIntent().getSerializableExtra("data");
		setTitle("帖子详情");
		setRightButtonResource(R.drawable.forum_posth);
		showRightButton();
		refreshView = findViewById(R.id.refreshScrollView);
		listView = findViewById(R.id.listView);
		
	}

	protected void initData() {
		setDetailData();
		ForumService.queryPostingDetail(item, this, activity);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof Posting) {
			item = (Posting) t[0];
			setPostingFileData();
			notifyDataSetChanged();
			setDetailData();
			ForumService.read(item.getPostingsid() + "", 1, this, activity);
		} else if (t[0] instanceof String) {
			item.setViews(item.getViews() + 1);
			setDetailData();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_right:
			doClickRightButton();
			break;
		case R.id.reply_send:
			doClickReplySendView(view);
			break;
		case R.id.iv_play:
			doClickPlayView(view);
			break;
		}

	}

	private void setDetailData() {
		ImageView iv_icon = findViewById(R.id.iv_icon);
		TextView tv_name = findViewById(R.id.tv_name);
		TextView tv_time = findViewById(R.id.tv_time);
		TextView tv_section = findViewById(R.id.tv_section);
		TextView tv_sec_title = findViewById(R.id.tv_sec_title);
		TextView tv_sec_content = findViewById(R.id.tv_sec_content);
		TextView tv_num_view = findViewById(R.id.tv_num_view);
		TextView tv_num_comment = findViewById(R.id.tv_num_comment);
		ImageUtils.circle(activity, item.getHeadphoto(), iv_icon);
		tv_name.setText(item.getUsername());
		tv_time.setText(TimeFormatUtils.format(item.getTime()));
		tv_section.setText("[" + item.getFirstsection() + ">" + item.getSecondsection() + "]");
		tv_sec_title.setText(item.getPostingsname());
		tv_sec_content.setText(item.getPostingscontent());
		tv_num_view.setText("浏览" + item.getViews());
		tv_num_comment.setText("评论" + item.getComment());
	}

	private void setPostingFileData() {
		GridView gridView = (GridView) view.findViewById(R.id.gridView);
		ImageView iv_video = (ImageView) view.findViewById(R.id.iv_video);
		List<String> urls = new ArrayList<String>();
		for (FileType file : item.getPostingsdetail()) {
			String type = file.getType();
			if (type.equals("IMAGE")) {
				urls.add(file.getUrl());
			} else if (type.equals("VIDEO")) {
				final String url = BaseConstant.RES_URL + file.getUrl();
				View iv_play = findViewById(R.id.iv_play);
				iv_play.setTag(url);
				((View) iv_play.getParent()).setVisibility(View.VISIBLE);
				ImageUtils.rect(url, iv_video);
			}
		}
		PhotoGridAdapter adapter = new PhotoGridAdapter(activity, urls);
		gridView.setAdapter(adapter);
	}

	private void doClickRightButton() {
		startActivityForResult(PostingCreateActivity.class, 0);
	}

	private void doClickReplySendView(View view) {
		Integer id = item.getPostingsid();
		EditText reply_content = (EditText) ((ViewGroup) view.getParent()).findViewById(R.id.reply_content);
		String content = reply_content.getText().toString();
		reply_content.setText(null);
		Posting item_old = (Posting) activity.getIntent().getSerializableExtra("data");
		CommentService.createComment(item_old, id + "", content, this, activity);
	}

	private void doClickPlayView(View view) {
		String url = view.getTag() + "";
		if (TextUtils.isEmpty(url)) {
			showToast("视频地址无效");
			return;
		}
		Intent intent = new Intent(activity, VideoPlayActivity.class);
		intent.putExtra("url", url);
		startActivity(intent);
	}

	private void notifyDataSetChanged() {
		PostingCommentAdapter adapter = new PostingCommentAdapter(activity, item.getPostingscomment());
		listView.setAdapter(adapter);
		refreshView.onRefreshComplete();
	}

	private OnRefreshListener<ScrollView> refreshListener = new OnRefreshListener<ScrollView>() {
		@SuppressWarnings("deprecation")
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			refreshView.setRefreshingLabel(activity.getResources().getString(R.string.refreshing_label));
			refreshView.onLoadComplete();
		}

	};

	public void onActivityResult(int arg0, int arg1, Intent intent) {
		if (hasRefresh(intent)) {
			setRefresh(true);
		}
	};
}
